// pages/index/talentMarket/talentMarket.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    showOffer:true,
    showOfferInfo:false,
    page: 1,
    count: 10,
    recruitList: [],
    jobList: [],
    active: 0,
    index: 0
  },
  onClick(e){
    const { recruitList, jobList } = this.data
    if (e.detail.index == 0){
      this.setData({
        showOffer: true,
        showOfferInfo: false,
        index: e.detail.index,
        page: 1,
      })

      if (!recruitList.length) {
        this.getRecruit()
      }
    }else{
      this.setData({
        showOffer: false,
        showOfferInfo: true,
        index: e.detail.index,
        page: 1,
      })

      if (!jobList.length) {
        this.getJobList()
      }
    }
  },
  // 招聘列表
  getRecruit(){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: app.d.severUrl + 'recruit_list',
      method: 'GET',
      data: {
        page: that.data.page,
      },
      success: res => {
        wx.hideLoading();
        if (res.data.code == 0) {
          var content = that.data.recruitList.concat(res.data.data);
          that.setData({
            recruitList: content
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg
          })
        }
      },
      fail: error => {
        wx.showToast({
          icon: 'none',
          title: error.data.msg
        })
      }
    })
  },
  //求职列表
  getJobList(){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: app.d.severUrl + 'jobwant_list',
      method: 'GET',
      data: {
        page: that.data.page,
      },
      success: res => {
        wx.hideLoading();
        if (res.data.code == 0) {
          var content = that.data.jobList.concat(res.data.data);
          that.setData({
            jobList: content
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg
          })
        }
      },
      fail: error => {
        wx.showToast({
          icon: 'none',
          title: error.data.msg
        })
      }
    })
  },

  makePhone(e){
    if (e.target.dataset.phone){
      wx.makePhoneCall({
        phoneNumber: e.target.dataset.phone
      })
    }else{
      wx.showToast({
        icon: 'none',
        title: "暂无联系电话"
      })
    }
    
  },
  
  onLoad (options) {
    var that = this;
    if (options.active == 1){
      that.setData({
        active: options.active,
        showOffer: false,
        showOfferInfo: true
      })
      that.getJobList();
    }else{
      that.getRecruit();
    }
    
  },

  onPullDownRefresh: function () {
   
    
  },

  onReachBottom: function () {
    var that = this;
    if (that.data.index == 0) {
      if (that.data.page * that.data.count > that.data.recruitList.length && that.data.recruitList.length > 0) {
        wx.showToast({
          title: '已加载全部数据',
        })
      } else if (that.data.recruitList.length > 0) {
        that.data.page = that.data.page + 1;
        that.getRecruit();
      }
    }else{
      if (that.data.page * that.data.count > that.data.jobList.length && that.data.jobList.length > 0) {
        wx.showToast({
          title: '已加载全部数据',
        })
      } else if (that.data.jobList.length > 0) {
        that.data.page = that.data.page + 1;
        that.getJobList();
      }
    }
  },
})